<?php
class OrdersBillingsController extends AppController {

	var $name = 'OrdersBillings';
	var $permissions = array(
		'add' => array('admin','manag'),
		'index' => array('admin','manag'),
		'view' => array('admin','manag'),
		'edit' => array('admin','manag'),
		'delete' => array('admin','manag'),
		'find' => array('admin','manag')
	);	
	public $presetVars = array(
		array('field' => 'pay_date', 'type' => 'value'),
		array('field' => 'employee_id', 'type' => 'value'),
		array('field' => 'pay_number', 'type' => 'like')
	);
	
	function index() {
		$this->OrdersBilling->recursive = 0;
		$this->set('ordersBillings', $this->paginate());
		$employees = $this->OrdersBilling->Employee->find('all');
		foreach ($employees as $key => $employee) {
			$employee_[$employee['Employee']['id']] = $employee['Employee']['name']. ' '.$employee['Employee']['surname']. ' ('.$employee['Employee']['address1'].'/'.$employee['Employee']['address3'].')';
		}
		$employees = $employee_;
		$this->set('employees',$employees);
	}
	
	public function find() {
		if(!empty($this->data)){
			$this->data['OrdersBilling']['pay_date'] =  $this->EuroDate->convert($this->data['OrdersBilling']['pay_date'],"Y-m-d");
		}		
		$this->Prg->commonProcess();
		$this->paginate['conditions'] = $this->OrdersBilling->parseCriteria($this->passedArgs);
		$this->set('ordersBillings', $this->paginate());
		$employees = $this->OrdersBilling->Employee->find('all');
		foreach ($employees as $key => $employee) {
			$employee_[$employee['Employee']['id']] = $employee['Employee']['name']. ' '.$employee['Employee']['surname']. ' ('.$employee['Employee']['address1'].'/'.$employee['Employee']['address3'].')';
		}
		$employees = $employee_;
		$this->set('employees',$employees);
		
		$this->autoRender = false;
		$this->render('index');
	}	

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__("Invalid orders billing", true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('ordersBilling', $this->OrdersBilling->read(null, $id));
	}

	function add() {
		if (!empty($this->data) && !isset($this->data['OrdersBilling']['save_to_database'])) {
			$date_from = $this->data['OrdersBilling']['date_from'];
			$date_to = $this->data['OrdersBilling']['date_to'];
			$this->data['OrdersBilling']['date_from'] = $this->EuroDate->convert($this->data['OrdersBilling']['date_from'],"Y-m-d");
			$this->data['OrdersBilling']['date_to'] = $this->EuroDate->convert($this->data['OrdersBilling']['date_to'],"Y-m-d");			
			$this->OrdersBilling->set($this->data);
			if ($this->OrdersBilling->validates($this->data,array('fields'=>array('date_from','date_to','employee_id')))) {
				
				$ordersSeries = $this->OrdersBilling->OrdersSeries->find('all',array(
					'conditions'=>array('OrdersSeries.employee_id'=>$this->data['OrdersBilling']['employee_id'],
						'OrdersSeries.execution_date BETWEEN ? AND ?' => array($this->data['OrdersBilling']['date_from'],$this->data['OrdersBilling']['date_to']),
						'OrdersSeries.status'=>2,
						'OrdersSeries.payment_status'=>2
				)));
				$employee = $this->OrdersBilling->Employee->read(null,$this->data['OrdersBilling']['employee_id']);
				
				$this->set(compact('ordersSeries','employee'));
				$this->autoRender = false;
				$this->render("add_confirm");
			} else {
				$this->data['OrdersBilling']['date_from'] = $date_from;
				$this->data['OrdersBilling']['date_to'] = $date_to;				
				$this->Session->setFlash(__("The orders billing could not be saved. Please, try again.", true));
			}
		}else if(isset(  $this->data['OrdersBilling']['save_to_database'])  && 
			$this->data['OrdersBilling']['save_to_database'] == 'confirm' && 
			$this->data['OrdersBilling']['pay_value'] != 0
		){
			$this->data['OrdersBilling']['pay_date'] = date('Y-m-d');
			$this->OrdersBilling->create();
			if($this->OrdersBilling->save($this->data)){
				$biling_id = $this->OrdersBilling->getLastInsertID();
				foreach ($this->data['OrdersSeries'] as $id => $value) {
					$update = array();
					$update['OrdersSeries']['id'] = $id;
					$update['OrdersSeries']['orders_billing_id'] = $biling_id;	
					$this->OrdersBilling->OrdersSeries->id = $id;			
					$this->OrdersBilling->OrdersSeries->save($update);
				}
				$this->data = null;
			}else{
				$this->Session->setFlash(__("The orders billing could not be saved. Please, try again.", true));
			}
			$this->redirect(array('action'=>'index'));
		}
		$employees = $this->OrdersBilling->Employee->find('all');
		foreach ($employees as $key => $employee) {
			$employee_[$employee['Employee']['id']] = $employee['Employee']['name']. ' '.$employee['Employee']['surname']. ' ('.$employee['Employee']['address1'].'/'.$employee['Employee']['address3'].')';
		}
		$employees = $employee_;
		$this->set(compact('employees'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__("Invalid orders billing", true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->OrdersBilling->save($this->data)) {
				$this->Session->setFlash(__("The orders billing has been saved", true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__("The orders billing could not be saved. Please, try again.", true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->OrdersBilling->read(null, $id);
		}
		$employees = $this->OrdersBilling->Employee->find('all');
		foreach ($employees as $key => $employee) {
			$employee_[$employee['Employee']['id']] = $employee['Employee']['name']. ' '.$employee['Employee']['surname']. ' ('.$employee['Employee']['address1'].'/'.$employee['Employee']['address3'].')';
		}
		$employees = $employee_;
		$this->set(compact('employees'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__("Invalid id for orders billing", true));
			$this->redirect(array('action'=>'index'));
		}
		$data['OrdersSeries']['orders_billing_id'] = '';
		if($this->OrdersBilling->OrdersSeries->updateAll(array('OrdersSeries.orders_billing_id'=>null),array('OrdersSeries.orders_billing_id'=>$id))){
			if ($this->OrdersBilling->delete($id)) {				
				$this->Session->setFlash(__("Orders billing deleted", true));
				$this->redirect(array('action'=>'index'));
			}
		}else{
			$this->Session->setFlash(__("The orders billing could not be deleted. Please, try again.", true));
		}		
		$this->Session->setFlash(__("Orders billing was not deleted", true));
		$this->redirect(array('action' => 'index'));
	}
}
?>